# -*- coding: iso-8859-1 -*-
"""
@newfield project: Project 
@project: yatter
@newfield description: Description 
@description: Twitter Client, based on Python 2.5

@Author: U{Andreas del Galdo<mailto:andreas.delgaldo@mac.com>}
@summary: Handles the access to everything concerning the network. Proxies can 
          be handled and sockets can be openend. Also HTTP-Requests are generated
          in this Module.
"""

import urllib2
import logging

def configConnection(proxy_ip, proxy_port, proxy_username, proxy_password):
    try:
        proxy = {}
        
        if ((proxy_ip == None) or (proxy_port == None)):
            logging.debug("tools.network.configConnection()::NO_PROXY_USED")           

            proxy = None
        else:
            logging.debug("tools.network.configConnection()::PROXY_USED")
            
            if (proxy_username == None):
                proxy_username = ""
                proxy_password = ""
            else:
                proxy_username = proxy_username + ":"
                proxy_password = proxy_password + "@"
                
            if (proxy_ip.find('https') != -1):
                proxy = {'https' : proxy_username + proxy_password + proxy_ip + ':' + proxy_port}
            else:
                proxy = {'http'  : proxy_username + proxy_password + proxy_ip + ':' + proxy_port}
            
            urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler(proxy)))
        return True
    except:
        logging.error("tools.network.configConnection()::ERROR")
        return False


def createRequest(url):
    try:
        logging.debug("tools.network.createRequest()")
        handler = urllib2.urlopen(url, None)
        return handler
    except:
        logging.error("tools.network.createRequest()::ERROR")
        return False
